package com.pjedu.nbmallshop.service.imp;

import com.pjedu.nbmallcommon.common.ServiceResultEnum;
import com.pjedu.nbmallcommon.utils.BeanUtil;
import com.pjedu.nbmallcommon.utils.PageResult;
import com.pjedu.nbmallshop.entity.MallCarousel;
import com.pjedu.nbmallshop.mapper.CarouselMapper;
import com.pjedu.nbmallshop.service.MallCarouselService;
import com.pjedu.nbmallshop.vo.MallIndexCarouselVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class MallCarouselServiceImpl implements MallCarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    //拿到当前页面数据
    @Override
    public PageResult getCarouselPage(HashMap pageUtil) {
        //这一步已经把页面数据给都搞定了,包括数据的内容、数据的分页、数据的排序
        List<MallCarousel> carousels = carouselMapper.findCarouselList(pageUtil);
        //其他所需要发送给前端的数据是给下面的分页模块所使用的
        int total = carouselMapper.getTotalCarousels();
        PageResult pageResult = new PageResult(carousels, total, Integer.valueOf(pageUtil.get("limit").toString()), Integer.valueOf(pageUtil.get("page").toString()));
        return pageResult;
    }
    //新增轮播图
    @Override
    public String saveCarousel(MallCarousel carousel) {
        if (carouselMapper.insertSelective(carousel) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    //更新轮播图
    @Override
    public String updateCarousel(MallCarousel carousel) {
        MallCarousel temp = carouselMapper.selectByPrimaryKey(carousel.getCarouselId());
        if (temp == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //请问:前台的当前时间和服务器的当前时间是否一样?
        temp.setCarouselRank(carousel.getCarouselRank());
        temp.setRedirectUrl(carousel.getRedirectUrl());
        temp.setCarouselUrl(carousel.getCarouselUrl());
        temp.setUpdateTime(new Date());
        if (carouselMapper.updateByPrimaryKeySelective(temp) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    //点击修改按钮,根据id查询数据
    @Override
    public MallCarousel getCarouselById(Integer id) {
        return carouselMapper.selectByPrimaryKey(id);
    }
    //批量删除
    @Override
    public Boolean deleteBatch(Integer[] ids) {
        if (ids.length < 1) {
            return false;
        }
        //删除数据
        return carouselMapper.deleteBatch(ids) > 0;
    }

    @Override
    public List<MallIndexCarouselVO> getCarouselsForIndex(int number) {
        List<MallIndexCarouselVO> mallIndexCarouselVOS = new ArrayList<>(number);
        List<MallCarousel> carousels = carouselMapper.findCarouselsByNum(number);
        if (!CollectionUtils.isEmpty(carousels)) {
            mallIndexCarouselVOS = BeanUtil.copyList(carousels, MallIndexCarouselVO.class);
        }
        return mallIndexCarouselVOS;
    }
}
